import { createRouter, createWebHistory } from 'vue-router'

// 定义需要登录才能访问的路由
const authRoutes = [
  '/home',
  '/user',
  '/charts',
  '/user',
  '/article',
  '/addArticle/:id'
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      component: () => import("@/views/login/login.vue")
    },
    {
      path: '/consloe',
      component: () => import('@/views/layout/index.vue'),
      redirect: '/home',
      children: [
        {
          path: '/home',
          component: () => import('@/views/page/home.vue')
        },
        {
          path: '/charts',
          component: () => import('@/views/page/charts.vue')
        },
        {
          path: '/user',
          component: () => import('@/views/page/user.vue')
        },
        {
          path: '/article',
          component: () => import('@/views/page/articleManage/article.vue')
        },
        {
          path: '/notes',
          component: () => import('@/views/page/articleManage/Notes.vue')
        },
        {
          path: '/category',
          component: () => import('@/views/page/category/category.vue')
        },
        {
          path: '/tags',
          component: () => import('@/views/page/category/tags.vue')
        },
        {
          path: '/comment',
          component: () => import('@/views/page/articleManage/Comments.vue')
        },
        {
          path: '/project',
          component: () => import('@/views/page/articleManage/Project.vue')
        },
        {
          path: '/message',
          component: () => import('@/views/page/message.vue')
        },
        {
          path: '/album',
          component: () => import('@/views/page/album.vue')
        },
        {
          path: '/myself',
          component: () => import('@/views/page/my/myselfInfo.vue')
        },
        {
          path: '/experince',
          component: () => import('@/views/page/my/studyExperince.vue')
        },
        {
          path: '/notify',
          component: () => import('@/views/page/notify.vue')
        },
        {
          path: '/friend',
          component: () => import('@/views/page/friend.vue')
        },
        /**
         * 添加文章
         */
        {
          path: '/addArticle/:id',
          component: () => import('@/views/add/AddArticle.vue')
        },
        /**
         * 添加动态
         */
        {
          path: '/addNotes/:id',
          component: () => import('@/views/add/addNotes.vue')
        },
        /**
         * 添加项目
         */
        {
          path: '/addProject/:id',
          component: () => import('@/views/add/AddProject.vue')
        },
        /**
         * 添加个人信息
         */
        {
          path: '/addMyInfo/:id',
          component: () => import('@/views/add/AddMyInfo.vue')
        },
        /**
         * 添加个人信息欢迎词
         */
        {
          path: '/addMyInfo/Welcome',
          component: () => import('@/views/add/AddMyInfoWelcome.vue')
        }
      ]
    }
  ],
})

// 全局前置守卫
router.beforeEach((to, from, next) => {
  // 检查目标路由是否需要登录权限
  if (authRoutes.some(route => {
    // 使用正则表达式匹配动态路由
    const pattern = new RegExp(`^${route.replace(/:[^/]+/g, '[^/]+')}$`)
    return pattern.test(to.path)
  })) {
    // 获取token
    const token = localStorage.getItem('token')
    if (!token) {
      // 如果没有token，重定向到登录页
      next('/')
      return
    }
    // 简单的token格式验证，假设token是JWT格式，至少应该包含两个点号（.）
    if (token.split('.').length!== 3) {
      // token格式不正确，重定向到登录页
      next('/')
      return
    }
  }
  // 不需要登录权限的路由或已登录，允许访问
  next()
})

export default router
